/**
 * Copyright 2011 Renato de Jesus Manzoni
 * 
 * SED - Software Engineering and Dependability Research Group
 * 
 *     http://www.sed.ic.unicamp.br/
 * 
 * Distributed Systems Laboratory (LSD) in the Institute of Computing (IC) at University of Campinas (UNICAMP)
 * 
 *     http://www.lsd.ic.unicamp.br/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package br.unicamp.ic.lsd.wscaa;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import br.unicamp.ic.lsd.wscaa.concurrent.ConcurrentExceptionSync;
import br.unicamp.ic.lsd.wscaa.config.ConfigurationLoader;
import br.unicamp.ic.lsd.wscaa.config.ConfigurationManager;
import br.unicamp.ic.lsd.wscaa.exception.RefreshException;

/**
 * @author Renato de Jesus Manzoni <rjmanzoni@gmail.com>
 * 
 */
public class WSCAManager {

    private List<Object> notifyList;
    // private boolean doneExceptionHandling;
    // TODO: criar uma thread deamon para recarregar os handlers a cada x
    // minutos?
    private ConfigurationManager configManager;
    private ConcurrentExceptionSync syncExcetions;

    private static Log logger = LogFactory.getLog(WSCAManager.class);

    // public static WSCAManager getInstance() {
    // if (instance == null)
    // instance = new WSCAManager();
    // return instance;
    // }

    // public WSCAManager() {
    // refresh();
    // syncExcetions = new ConcurrentExceptionSync(this);
    // }

    /**
     * @param xmlPath
     */
    public WSCAManager(String xmlPath) {

	refresh();
	configManager = new ConfigurationManager(
		ConfigurationLoader.loadConfiguration(xmlPath));
	syncExcetions = new ConcurrentExceptionSync(this);
    }

    public void addNotifyMe(Object me) {
	synchronized (notifyList) {
	    notifyList.add(me);
	}
    }

    public void notifyObjects() {
	synchronized (notifyList) {
	    // this.doneExceptionHandling = true;
	    for (Iterator<Object> iterator = notifyList.iterator(); iterator
		    .hasNext();) {
		Object obj = (Object) iterator.next();
		synchronized (obj) {
		    logger.debug("Notifyng threads to weak up");
		    obj.notifyAll();
		}
	    }
	}
	// notifyList.clear();
    }

    public void start(long timeoutExceptionResolution) {
	// TODO: acho que seria mais interessante definir uma interface para
	// invoka o metodo que implementa ela. Isso eh para desvincular a
	// estrategia de sincronizacao

	logger.debug("Refreshing queue exceptions and notifyList threads");

	try {
	    syncExcetions.refresh(timeoutExceptionResolution);
	} catch (RefreshException e) {
	    logger.error("Exception in Refresh queue exception", e);
	    throw new RuntimeException(e);
	}

	refresh();
    }

    private void refresh() {
	// doneExceptionHandling = false;
	if (notifyList == null)
	    notifyList = Collections.synchronizedList(new ArrayList<Object>());
	else
	    notifyList.clear();
    }

    public boolean hasDoneExceptionHandling() {
	return syncExcetions.isDoneExceptionsHandling();
    }

    // public void setDoneExceptionHandling(boolean doneExceptionHandling) {
    // this.doneExceptionHandling = doneExceptionHandling;
    // }

    /**
     * @return the syncExcetions
     */
    public ConcurrentExceptionSync getSyncExcetions() {
	return syncExcetions;
    }

    /**
     * @return the configManager
     */
    public ConfigurationManager getConfigManager() {
	return configManager;
    }

}
